Page({
  data: {
    collectedItems: [],
  },

  onLoad(options) {
    this.getCollectedItems();
  },

  getCollectedItems: function() {
    try {
      const collectedItems = wx.getStorageSync('collectedItems') || [];
      this.setData({
        collectedItems: collectedItems
      });
    } catch (e) {
      console.error("获取缓存数据失败", e);
    }
  },

  // 删除对应的缓存内容
  detele: function(event) {
    const index = event.currentTarget.dataset.index;

    // 弹出确认对话框
    wx.showModal({
      title: '确认删除',
      content: '您确定要删除该项吗？',
      success: (res) => {
        if (res.confirm) {
          let collectedItems = this.data.collectedItems;

          if (index > -1) {
            collectedItems.splice(index, 1); // 删除该项
          }

          try {
            wx.setStorageSync('collectedItems', collectedItems);
          } catch (e) {
            console.error("设置缓存数据失败", e);
          }

          this.setData({
            collectedItems: collectedItems
          });
        } else if (res.cancel) {
          console.log('用户取消删除');
        }
      }
    });
  },

  // 点击项目，跳转到详情页面
  goToDetail: function(event) {
    const id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../love_details/love_details?itemId=${id}`,
    });
  },

  
});
